<extend name="layout/edit" />

<block name="HEADER">
   <style type="text/css">
	div[lay-id='gridAuthorize']  .layui-table-cell{
		height: auto !important; 
		overflow: auto; 
		white-space: normal;
	}
	div[lay-id='gridAuthorize']  .layui-table-cell label{
		margin-right: 10px;
	}
	div[lay-id='gridAuthorize']   .layui-form-checkbox{
		display: none;
	}
   </style>
</block>

<block name="CONTENT">
    <control:Grid id="gridAuthorize"/>
</block>

<block name="FOOTER">
   <script type="text/javascript">
   	jQuery.fn.extend({
		serializeJsons: function() {
			var $th = this;
			var Input = $th.find("input");
			var Select = $th.find("select");
			var Textarea = $th.find("textarea");
			var Checkbox = $th.find("input[type='checkbox']");
			var Radio = $th.find("input[type='radio']");
			var Search = $th.find("div[type='search']");
			var obj = {};
			function treatmen(dom) {
				$.each(dom, function() {
					if(this.type == "button" || this.type == "checkbox" || this.type == "radio") {
					    return true;
					};
					if($(this).parents('.layui-table').length >0 ){
						return true;
					}
					if($(this).attr("form-datatype")=="number" && this.name){
					    obj[this.name] = Number($.delcommafy($.trim(this.value))); 
					}else if($(this).attr("type")=="number" && this.name){
					    obj[this.name] = Number($.trim(this.value)); 
					}else if(this.name){
					    obj[this.name] = $.trim(this.value); 
					}
				});
			};
			function twoTreatment(dom) {
				var names = $.unique( $.map(dom, function(h) {
				    if( $(h).attr("name")!="layTableCheckbox" ){
						return $(h).attr("name")
					}
				}) );
				$.each(names, function(index, o) {
					var box = $th.find("input[name='" + o + "']:checked");
					obj[o]=$.map(box, function(h) { return  $.trim(h.value)   }).join(',');
				});
			};
			function search(dom){
	            $.each(dom, function() {
	                if($(this).attr("name")){
	                    obj[$(this).attr("name")] = $.trim($(this).attr("data-value")); 
	                }
	            });
	        }
			treatmen(Input);
			treatmen(Textarea);
			treatmen(Select);
			twoTreatment(Checkbox);
			twoTreatment(Radio);
			search(Search);
			//转义script标签
			return $.scriptEncode(obj);
		}
	});
   	
    var  page=(function(ns){
        
        ns.org_id = $.getRequest('oid') || -1;
        
        //页面初始化
        ns.init=function(){
    		gridAuthorize.load([{field: "org_id", value:ns.org_id}]);
    		gridAuthorize.loadSuccess(function(res){
	        	ns.initAuthorizeCheck(res.data);
	        });
        }
       
      	ns.actionTemplet = function(e){
	 		var funcCode = e.data.func_code;
	 		var values = (e.value || '').split('|');
	 		return $.map(values, function(h) {
	 			var vals  = (h || '').split('-');
	 			if(vals[0]){
	 				return '<label><input type="checkbox" name="'+funcCode+'"  value="'+vals[0]+'" >'+vals[1]+'</label>';
	 			}
	 		}).join('');
	 	}
        
		//授权保存
	    ns.save=function (index){
	        var data = $("#gridAuthorize").serializeJsons();
	        var obj={};
	        for(var i in data){
	        	if(data[i]){
	        		obj[i] = data[i];
	        	}
	        }
	        var param={
	        	actions:$.isEmptyObject(obj) ?  "" : JSON.stringify(obj),
	        	org_id:ns.org_id
	        }
	        $.ajaxs("/admin/soft/system_user/action",param,function(res){
	        	$.toast(1,"授权成功！");
	        });
	    }
	    
	    //初始化授权选中
	    ns.initAuthorizeCheck=function(rows){
	    	$.ajaxs('/admin/soft/group/getAction',{
	    		org_id: ns.org_id
	    	}).then(function(res){
	    		var actionObj = res.data != "''" && res.data ?  JSON.parse(res.data) : {};
	    		$.each(rows,function(index,o){
	        		var actions = String(actionObj[o.func_code]).split(',');
	        		$.map(actions, function(h) {
	        			$('#gridAuthorize').find("input[name='"+o.func_code+"'][value='"+h+"']").prop("checked",true);
	        		});
	        	});
	        	layui.form.render();
	    	});
	    }
	    
	    //清空所有
	    ns.emptyCheckAll=function(func_code){
	    	$('#gridAuthorize').find("label>input[type='checkbox']").prop("checked",false);
	    	layui.form.render();
	    }
	    
	    //选中所有
	    ns.checkAll=function(func_code,is){
	        $('#gridAuthorize').find("label>input[type='checkbox']").prop("checked",true);
	        layui.form.render();
	    }
	    
	    //选中所有查询
	    ns.checkAllQuery=function(func_code,is){
	    	$('#gridAuthorize').find("input[value='00']").prop("checked",true);
	    	layui.form.render();
	    }
	    
	    //行全选和清空
	    ns.rowCheck=function(func_code,is){
	    	$('#gridAuthorize').find("input[name='"+func_code+"']").prop("checked",is == "true" ? true : false);
	    	layui.form.render();
	    }
	    
	    //编辑权限
	    ns.editAction=function(){
	        var selectedRow= gridAuthorize.getSelectedRows()[0];
	        var filter={
	            url:"system/pageMng/edit",
	            oid:selectedRow.nav_id,
	            mode:2
	        }
	        $.openWin(filter, "编辑父页面",['750px', '500px']); 
	    }        
        
        return ns;
    }({}));
    
    //加载当前页面需要的控件和配置
    $(function(){  page.init();  });
   </script>
</block>



